<template>
  <div class="form-rules-container layout-pd" style="text-align: left">
    <el-form :model="queryParams" size="default" inline label-width="100px" class="mt35 mb35">
      <el-form-item label="菜单名称" style="text-align: left">
        <el-input v-model="queryParams.name" placeholder="请输入菜单名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="menuListData">搜索</el-button>
        <el-button >重置</el-button>
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <el-button type="primary" @click="handleOpen">新增</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div>
      <el-table :data="menuData" row-key="id">
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="菜单名称" prop="name"></el-table-column>
        <el-table-column label="上级菜单" prop="paretName"></el-table-column>
        <el-table-column label="接口地址" prop="path"></el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleOpen(scope.row)">修改</el-button>
            <el-button link type="primary" size="small" @click="delMenu(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
        v-model="dialogVisible"
        title="编辑菜单"
        width="30%"
        :before-close="handleClose"
    >
      <el-form :model="menuForm" ref="menuFormRef" :rules="rules">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="menuForm.name" placeholder="填写角色名称"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="menuForm.pid" placeholder="上级菜单">
            <el-option v-for="item in menuData" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接口地址" v-if="menuForm.pid">
          <el-input v-model="menuForm.path" placeholder="填写访问地址"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
			<span class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="submitForm">
				提交
				</el-button>
			</span>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import {getCurrentInstance, onMounted, reactive, toRefs} from "vue";
import {ElMessage} from "element-plus";

export default {
  setup() {
    const axios = getCurrentInstance().appContext.config.globalProperties.$axios;
    const state = reactive({
      queryParams: {},
      menuData: [],
      menuForm: {},
      dialogVisible: false
    })
    const handleOpen = (row) => {
      state.dialogVisible = true
      if (row.id) {
        state.menuForm = row
      } else {
        state.menuForm = {}
      }
    }
    const handleClose = () => {
      state.dialogVisible = false
      state.menuForm = {}
    }
    const delMenu = async (id) => {
      await axios.delete(`/menu/${id}`)
      ElMessage.success('删除成功')
      await menuListData()
    }
    const menuListData = async () => {
      let res = await axios.post('/menu', state.queryParams);
      state.menuData = res.data.data;
    }
    onMounted(() => {
      menuListData()
    })
    return {
      ...toRefs(state),
      menuListData,
      handleOpen,
      handleClose,
      delMenu
    }
  }
}
</script>
